/**
 * Created by rockyren on 14/11/18.
 */
describe('Unit:控制器测试',function(){
  beforeEach(module('productApp'));
  var $httpBackend;
  beforeEach(inject(function(_$httpBackend_){
    $httpBackend = _$httpBackend_;
  }));

  /**
   * productIndexController:商品首页的控制器
   */
  describe('productIndexController',function(){

    var productIndexController, scope;
    beforeEach(inject(function($controller,$rootScope){
      scope = $rootScope.$new();
      productIndexController = $controller('productIndexController',{$scope: scope});
    }));

    it('当控制器加载后,应该能请求商品首页信息',function(){
      var resObj = [
          {
            "id": 1,
            "label": "recommend",
            "productList": [
              {
                "id": 120,
                "name": "card",
                "image": "card.jpg",
                "price": 2333
              },
              {
                "id": 121,
                "name": "baby",
                "image": "baby.jpg",
                "price": 1000
              }
            ]
          },
          {
            "id": 2,
            "label": "class1",
            "productList": [
              {
                "id": 122,
                "name": "rocky",
                "image": "rocky.jpg",
                "price": 3222
              }
            ]
          }
        ];
      $httpBackend.expectGET('api/product').respond(200,resObj);
      $httpBackend.flush();

      //组织一个recommend格式的数据
      var expectRecommend = {
        id: 1,
        main:  {
          "id": 120,
          "name": "card",
          "image": "card.jpg",
          "price": 2333
        },
        minor: [
          {
            "id": 121,
            "name": "baby",
            "image": "baby.jpg",
            "price": 1000
          }
        ]

      };


      var expectClassification = [
        {
        "id": 2,
        "label": "class1",
        "productList": [
          {
            "id": 122,
            "name": "rocky",
            "image": "rocky.jpg",
            "price": 3222
          }
        ]
      }];

      expect(expectRecommend).toEqual(scope.recommend);
      expect(expectClassification).toEqual(scope.classification);

      //expect(scope.recommend).toEqual(resObj.recommend);
      //expect(scope.classification).toEqual(resObj.classification);
    });
  });

  describe('productInfoController',function(){
    var productInfoController,scope,resObj;
    beforeEach(inject(function($controller,$rootScope,$routeParams){
      scope = $rootScope.$new();
      $routeParams.productId = 2;
      productInfoController = $controller('productInfoController',{$scope: scope});

      //在前面就给一个请求响应
      resObj = {
        "baseInfo": {
          "id": 124,
          "name": "rocky",
          "image": "rocky.jpg",
          "price": 2333,
          "description": "hehe",
          "amount": 12
        }
      };

      $httpBackend.expectGET('api/product/2').respond(200,resObj);
      $httpBackend.flush();

    }));

    it('当加载控制后,应该能请求特定的具体商品信息',function(){

      expect(scope.productInfo).toEqual(resObj.baseInfo);
    });

    /*
    it('测试方法:minus和plus',function(){
      scope.minus();
      expect(1).toEqual(scope.count);
      expect(2).not.toEqual(scope.count);
      scope.plus();
      expect(2).toEqual(scope.count);
      scope.count = 12;
      scope.plus();
      expect(12).toEqual(scope.count);
      expect(13).not.toEqual(scope.count);

    });*/


    it('测试minus和plus',function(){
      scope.Number.minus();
      expect(scope.Number.getCount()).toEqual(1);
      expect(scope.Number.getCount()).not.toEqual(2);

      scope.Number.plus();
      expect(scope.Number.getCount()).toEqual(2);
      for(var i=2;i<=12;i++) {
        scope.Number.plus();
      }
      expect(scope.Number.getCount()).toEqual(12);

      scope.Number.plus();
      expect(scope.Number.getCount()).not.toEqual(13);
      expect(scope.Number.getCount()).toEqual(12);

    });

  });
});